package object2;

import java.lang.Override;

/**
 * 多态数组
 */
public class PolyArray {

    public static void main(String[] args) {
        /*
            多态数组：数组的定义类型为父类类型，里面保存的实际元素类型为子类类型
            例：现有一个继承结构如下：创建一个Person对象、2个Student对象和2个Teacher对象，统一放在数组中，并调用每个对象的say()方法
                如何调用子类特有的方法：Teacher有一个teach Student有一个study
         */
        PolyPerson polyPerson = new PolyPerson("普通人1", 20);
        PolyTeacher t1 = new PolyTeacher("张老师", 30, 8000.0);
        PolyTeacher t2 = new PolyTeacher("王老师", 40, 10000.0);

        PolyStudent s1 = new PolyStudent("张三", 20, 90);
        PolyStudent s2 = new PolyStudent("李四", 21, 85);

        PolyPerson[] polyPeople = new PolyPerson[5];
        polyPeople[0] = polyPerson;
        polyPeople[1] = s1;
        polyPeople[2] = s2;
        polyPeople[3] = t1;
        polyPeople[4] = t2;

        //遍历数组，调用 公用say()方法
        for (int i = 0; i < polyPeople.length; i++) {
            //person[i] 编译类型是 Person,运行类型是是根据实际情况有JVM来判断
            System.out.println(polyPeople[i].say());
            //向下转型，调用独有方法
            if (polyPeople[i] instanceof PolyStudent) {
                PolyStudent student = (PolyStudent)polyPeople[i];
                student.study();
            } else if (polyPeople[i] instanceof PolyTeacher) {
                PolyTeacher teacher = (PolyTeacher)polyPeople[i];
                teacher.teach();
            } else if (polyPeople[i] instanceof PolyPerson) {
                System.out.println("这是PolyPerson...");
            } else {
                System.out.println("类型有误请检查");
            }
        }

    }
}

class PolyPerson {

    private String name;

    private int age;

    public PolyPerson(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge(){
        return age;
    }

    public void setAge(int age){
        this.age = age;
    }

    public String say() {
        return name + "\t" + age;
    }
}

class PolyTeacher extends PolyPerson {

    private double salary;

    public PolyTeacher(String name, int age, double salary) {
        super(name, age);
        this.salary = salary;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    @Override
    public String say() {
        return "老师" + super.say() + " salary=" + salary;
    }

    public void teach() {
        System.out.println("Teacher " + getName() + " is teaching");
    }

}

class PolyStudent extends PolyPerson {

    private double score;

    public PolyStudent(String name, int age, double score) {
        super(name, age);
        this.score = score;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    @Override
    public String say() {
        return "学生" + super.say() + " score=" + score;
    }

    public void study() {
        System.out.println("Student " + getName() + " is studying");
    }
}